package com.woniuxy.project.send.config.direct;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitDirectConfig {
    /*
     * 创建 Topic 交换机（主题模式）
     * 1、创建一个交换机：topic-exchange
     * 2、创建两个队列：queue1-topic, queue2-topic
     * 3、将两个队列绑定到交换机    路由key       交换机
     *      queue1-topic       user.*       topic-exchange
     *      queue2-topic       pers.#       topic-exchange
     */

    @Bean
    public TopicExchange topicExchange() {
        // 交换机持久化：true  自动删除：false
        return new TopicExchange("topic-exchange", true, false);
    }

    @Bean
    public Queue queueTopic1() {
        // 队列持久化配置：第二个参数设为true，确保消息在服务器重启后不会丢失
        return new Queue("queue1-topic", true);
    }

    @Bean
    public Queue queueTopic2() {
        // 队列持久化配置：第二个参数设为true，确保消息在服务器重启后不会丢失
        return new Queue("queue2-topic", true);
    }

    @Bean
    public Binding topicBinding1(Queue queueTopic1, TopicExchange topicExchange) {
        return BindingBuilder.bind(queueTopic1).to(topicExchange).with("user.*");
    }

    @Bean
    public Binding topicBinding2(Queue queueTopic2, TopicExchange topicExchange) {
        return BindingBuilder.bind(queueTopic2).to(topicExchange).with("pers.#");
    }

}
